package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContractVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(long userId, Integer friendUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(friendUserId));
        //1. 查询两者之间是不是朋友
        if(!mongoTemplate.exists(query, Friend.class)){
            //如果本身就不是朋友，才插入记录
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(friendUserId.longValue());
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }

        query = new Query(Criteria.where("userId").is(friendUserId).and("friendId").is(userId));
        //1. 查询两者之间是不是朋友
        if(!mongoTemplate.exists(query, Friend.class)){
            //如果本身就不是朋友，才插入记录
            Friend friend = new Friend();
            friend.setUserId(friendUserId.longValue());
            friend.setFriendId(userId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }
    }

    @Override
    public PageResult queryContractList(Long userId,Integer page, Integer pagesize, String keyword) {
        //1.查看当前登陆者好友
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        Long   counts = mongoTemplate.count(query, Friend.class);

        //2. 遍历所有的朋友，查找朋友的信息
        List<ContractVo> userInfoList = new ArrayList<>();
        if(friendList!=null){
            for (Friend friend : friendList) {
                //查询朋友的信息
                UserInfo userInfo = userInfoMapper.findById(friend.getFriendId());
                ContractVo contractVo = new ContractVo();
                //属性拷贝
                BeanUtils.copyProperties(userInfo,contractVo);
                contractVo.setUserId(friend.getFriendId().toString());
                if(!StringUtils.isEmpty(keyword)){
                    if(userInfo.getNickname().contains(keyword)){
                        userInfoList.add(contractVo);
                    }
                }else{
                    userInfoList.add(contractVo);
                }
            }
        }

        PageResult pageResult = new PageResult(page,pagesize,counts.intValue(),userInfoList);

        return pageResult;
    }
}
